<?php

namespace App\BO\OrderStatus;

use App\BO\OrderStatus\Meta\UserApplyRefundInfo;
use App\BO\OrderStatus\Meta\UserCancelInfo;
use App\BO\OrderStatus\State\AOrderState;
use App\BO\PaymentState\Pay\PayInfo;
use App\Entities\OrderEntity;
use Illuminate\Support\Facades\Log;

class OrderStateContext implements IOrderFlow
{
    /**
     * 状态实例数组
     * @var AOrderState[]
     */
    private static $instances = [];

    /**
     * @var OrderEntity
     */
    private $order;

    /**
     * 订单状态
     * @var AOrderState
     */
    private $orderState;

    /**
     * @var OrderStateModifyStore
     */
    private $orderStateModifyStore;


    /**
     * @param $className
     * @return AOrderState
     */
    public static function getInstance($className)
    {
        if (class_exists($className)) {
            if (!isset(self::$instances[$className])) {
                self::$instances[$className] = new $className();
            }
            return self::$instances[$className];
        } else {
            $str = "异常：请求状态类 $className 不存在";
            Log::warning($str);
            return null;
        }
    }

    /**
     * @return AOrderState
     */
    public function getOrderState(): AOrderState
    {
        return $this->orderState;
    }

    /**
     * @param AOrderState $orderState
     */
    public function setOrderState(AOrderState $orderState): void
    {
        $this->orderState = $orderState;
        $this->orderState->setStateContext($this);
        $this->orderState->setOrderStateModifyStore($this->orderStateModifyStore);
    }

    /**
     * @return OrderEntity
     */
    public function getOrder(): OrderEntity
    {
        return $this->order;
    }

    /**
     * @param OrderEntity $order
     */
    public function setOrder(OrderEntity $order): void
    {
        $this->order = $order;
    }

    /**
     * @param OrderStateModifyStore $orderStateModifyStore
     */
    public function setOrderStateModifyStore(OrderStateModifyStore $orderStateModifyStore): void
    {
        $this->orderStateModifyStore = $orderStateModifyStore;
    }

    /**
     * 安始化订单
     */
    public function init()
    {
        $this->orderState->init();
    }

    /**
     * 支付动作
     * @param PayInfo $payInfo
     */
    public function pay(PayInfo $payInfo)
    {
        $this->orderState->pay($payInfo);
    }

    /**
     * 分配订单动作
     * @return void
     */
    public function distribute()
    {
        $this->orderState->distribute();
    }

    /**
     * 服务动作
     * @return void
     */
    public function service()
    {
        $this->orderState->service();
    }

    /**
     * 退款动作
     */
    public function refund()
    {
        $this->orderState->refund();
    }

    /**
     * 评价动作
     * @return void
     */
    public function comment()
    {
        $this->orderState->comment();
    }

    /**
     * 过期末支付，将订单设置关闭
     * @return void
     */
    public function expire()
    {
        $this->orderState->expire();
    }

    public function userCancel(UserCancelInfo $userCancelInfo)
    {
        $this->orderState->userCancel($userCancelInfo);
    }

    /**
     * 用户申请退款
     * @param UserApplyRefundInfo $userApplyRefundInfo
     * @return void
     */
    public function applyRefund(UserApplyRefundInfo $userApplyRefundInfo)
    {
        $this->orderState->applyRefund($userApplyRefundInfo);
    }

    /**
     * 管理员发起退款
     */
    public function adminApplyRefund()
    {
        $this->orderState->adminApplyRefund();
    }

    /**
     * 向第三方平台正式发起退款申请
     * @return void
     */
    public function applyThirdPartRefund()
    {
        $this->orderState->applyThirdPartRefund();
    }

    /**
     * 管理员拒绝退款
     * @return mixed
     */
    public function adminRejectRefund()
    {
        $this->orderState->adminRejectRefund();
    }

    /**
     * @inheritDoc
     */
    public function userCancelledRefund()
    {
        $this->orderState->userCancelledRefund();
    }

    /**
     * @inheritDoc
     */
    public function serviceStart()
    {
        $this->orderState->serviceStart();
    }

    /**
     * @inheritDoc
     */
    public function serviceFinish()
    {
        $this->orderState->serviceFinish();
    }

    /**
     * @inheritDoc
     */
    public function report()
    {
        $this->orderState->report();
    }

    /**
     * @inheritDoc
     */
    public function cleanerCancelReport()
    {
        $this->orderState->cleanerCancelReport();
    }

    /**
     * @inheritDoc
     */
    public function adminAgreeReport()
    {
        $this->orderState->adminAgreeReport();
    }

    /**
     * @inheritDoc
     */
    public function adminRejectReport()
    {
        $this->orderState->adminRejectReport();
    }
}
